{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sentence Reversal\n",
    "\n",
    "## Problem\n",
    "\n",
    "Given a string of words, reverse all the words. For example:\n",
    "\n",
    "Given:\n",
    "    \n",
    "    'This is the best'\n",
    "\n",
    "Return:\n",
    "\n",
    "    'best the is This'\n",
    "\n",
    "As part of this exercise you should remove all leading and trailing whitespace. So that inputs such as:\n",
    "\n",
    "    '  space here'  and 'space here      '\n",
    "\n",
    "both become:\n",
    "\n",
    "    'here space'\n",
    "\n",
    "## Solution\n",
    "\n",
    "Fill out your solution below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rev_word(s):\n",
    "    return \" \".join(reversed(s.split()))\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'go? to ready you are John, Hi'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev_word('Hi John,   are you ready to go?')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'before space'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev_word('    space before')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'after space'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rev_word('space after     ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Learn\n",
    "- \" \".join()\n",
    "- reversed()\n",
    "- s.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nInit signature: reversed(self, /, *args, **kwargs)\\nDocstring:     \\nreversed(sequence) -> reverse iterator over values of the sequence\\n\\nReturn a reverse iterator\\nType:           type\\n'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reversed()\n",
    "'''\n",
    "Init signature: reversed(self, /, *args, **kwargs)\n",
    "Docstring:     \n",
    "reversed(sequence) -> reverse iterator over values of the sequence\n",
    "\n",
    "Return a reverse iterator\n",
    "Type:           type\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "_____"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test Your Solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ALL TEST CASES PASSED\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "RUN THIS CELL TO TEST YOUR SOLUTION\n",
    "\"\"\"\n",
    "\n",
    "from nose.tools import assert_equal\n",
    "\n",
    "class ReversalTest(object):\n",
    "    \n",
    "    def test(self,sol):\n",
    "        assert_equal(sol('    space before'),'before space')\n",
    "        assert_equal(sol('space after     '),'after space')\n",
    "        assert_equal(sol('   Hello John    how are you   '),'you are how John Hello')\n",
    "        assert_equal(sol('1'),'1')\n",
    "        print (\"ALL TEST CASES PASSED\")\n",
    "        \n",
    "# Run and test\n",
    "t = ReversalTest()\n",
    "t.test(rev_word)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Good Job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
